package com.loop.unclelend.ui.activity

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Handler
import android.os.Looper
import android.view.View
import com.loop.unclelend.R
import com.loop.unclelend.base.app.AppStackManager
import com.loop.unclelend.databinding.UiStepsWaitBinding
import com.loop.unclelend.pro.ProActivity
import com.loop.unclelend.utils.event.UncleEventData
import com.loop.unclelend.utils.event.UncleEventHelper
import com.loop.unclelend.utils.event.UncleEventType
import com.loop.unclelend.utils.helper.SharedHelper
import com.loop.unclelend.utils.thread.TimingThread

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/26 18:45
 * @Annotation：进件信息完成等待
 */
class Act_Steps_Wait: ProActivity(), TimingThread.ITimingThreadListener {
    lateinit var binding: UiStepsWaitBinding
    private var timeThread: TimingThread? = null
    //等待时长
    private var countdown = 9

    override fun getUncleRootView(): View {
        binding = UiStepsWaitBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initUncleView() {
        setUncleTitleBarColor(Color.parseColor("#FFFFFFFF"))
        setUncleTextTitle("")
        setUncleBackBtnVisibility(View.INVISIBLE)
        setUncleRightBtnRes(R.mipmap.icon_kefu)
    }

    override fun initUncleListener() {
        //联系客服
        rootViewBinding.imgKefu.setOnClickListener {
            showUncleKefuDialog("Step Complete Wait Page")
            UncleEventHelper.addUncleRequestEventAction(UncleEventType.CLICK.UNCLE_CLICK_CUSTOMER,"Step Complete Wait Page")
        }
    }

    override fun initUncleData() {
        startTimeThread()
    }

    /**
     * 启动倒计时
     */
    private fun startTimeThread() {
        if (timeThread != null) {
            timeThread!!.stopThread()
        }
        //随机 10秒 等待时长
        countdown = 9
        showUncleTipsText()
        if (timeThread == null) {
            timeThread = TimingThread()
            timeThread!!.setThreadDelayMillis(1000)
            timeThread!!.setLoadListener(this)
        }
        timeThread!!.startThread()
    }

    override fun onLoading() {
        if (countdown <= 1){
            countdown = 9
            timeThread!!.stopThread()
            showUncleLoading()
            showUncleSuccess()
            return
        }
        countdown--
        showUncleTipsText()
    }

    /**
     * 设置文本
     */
    @SuppressLint("SetTextI18n")
    private fun showUncleTipsText(){
        binding.txtCountdown.text = "${countdown}s"
        showUncleStepStatusView()
    }

    private fun showUncleSuccess(){
        Handler(Looper.getMainLooper()).postDelayed({
            hideUncleLoading()
            //设置未显示过指引dialog
            SharedHelper.saveUncleSPData(SharedHelper.UNCLE_SPKEY.UNCLE_DIALOG_GUIDE_SHOW,true)
            publishEventData(UncleEventData(UncleEventData.TYPE.UNCLE_LOAN_LIMIT_REFRESH))
            //返回首页
            AppStackManager.finishAllActivity(Act_Main::class.java)
        },500)
    }

    /**
     * 显示状态
     */
    private fun showUncleStepStatusView(){
        if (countdown in 7..9){
            binding.imgStep1.setImageResource(R.mipmap.icon_wait_step2)
            binding.txtStep1.setTextColor(Color.parseColor("#FF000000"))

            binding.imgStep2.setImageResource(R.mipmap.icon_wait_countdown)
            binding.txtStep2.setTextColor(Color.parseColor("#FF000000"))

            binding.imgStep3.setImageResource(R.mipmap.icon_wait_countdown)
            binding.txtStep3.setTextColor(Color.parseColor("#FF000000"))
            return
        }

        if (countdown in 4..6){
            binding.imgStep1.setImageResource(R.mipmap.icon_wait_step1)
            binding.txtStep1.setTextColor(Color.parseColor("#FFB6B6B6"))

            binding.imgStep2.setImageResource(R.mipmap.icon_wait_step2)
            binding.txtStep2.setTextColor(Color.parseColor("#FF000000"))

            binding.imgStep3.setImageResource(R.mipmap.icon_wait_countdown)
            binding.txtStep3.setTextColor(Color.parseColor("#FF000000"))
            return
        }
        binding.imgStep1.setImageResource(R.mipmap.icon_wait_step1)
        binding.txtStep1.setTextColor(Color.parseColor("#FFB6B6B6"))

        binding.imgStep2.setImageResource(R.mipmap.icon_wait_step1)
        binding.txtStep2.setTextColor(Color.parseColor("#FFB6B6B6"))

        binding.imgStep3.setImageResource(R.mipmap.icon_wait_step2)
        binding.txtStep3.setTextColor(Color.parseColor("#FF000000"))
    }

    override fun onStart() {
        super.onStart()
        UncleEventHelper.addUncleRequestEventAction(UncleEventType.PAGE.UNCLE_PAGE_STEP_CHECK_WAIT_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        UncleEventHelper.addUncleRequestEventAction(UncleEventType.PAGE.UNCLE_PAGE_STEP_CHECK_WAIT_END)
        if (timeThread != null) {
            timeThread!!.stopThread()
            timeThread = null
        }
    }
}